package com.fourpigeon.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fourpigeon.utils.COSUtils;
import com.fourpigeon.utils.LocalDateTimeSerializerUtils;
import com.qcloud.cos.http.HttpMethodName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 交易贴完整信息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tb_blog_deal")
public class BlogDeal{
    /**
     * 主键
     */
    @TableId(value = "id",type = IdType.AUTO)
    private Long id;
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 用户名称
     */
    private String userName;
    /**
     * 用户头像
     */
    private String icon;
    /**
     * 装备类型：0：球拍，1：装备，2：羽毛球
     */
    private Short type;
    /**
     * 装备名称
     */
    private String name;
    /**
     * 价格
     */
    private String price;
    /**
     * 联系方式
     */
    private String contact;
    /**
     * 补充内容
     */
    private String content;
    /**
     * 图片，上限9张
     */
    private String images;
    /**
     * 状态码：0正常，1：被举报，2：禁止查看
     */
    private Short status = 0;
    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    /**
     * 图片url
     */
    private String iconUrl = "";

    /**
     * 图片url
     */
    private String imgsUrl = "";


    /**
     *
     * @param user 用户对象
     */
    public void set(Long id,User user){
        this.id = id;
        this.userId = user.getId();
        this.userName = user.getName();
        this.icon = user.getIcon();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public BlogDeal matchAndUpdate(BlogDealDTO blogDealDTO){
        //结果列表，用于返回不同的元素的key
        List<Object> resultList = new ArrayList<>();
        Map<String, Object> oriMap = BeanUtil.beanToMap(this);
        Map<String, Object> newMap = BeanUtil.beanToMap(blogDealDTO);
        for (String key : newMap.keySet()) {
            if(key.equals("iconUrl") || key.equals("imgsUrl")) continue;
            if (!newMap.get(key).equals(oriMap.get(key))){
                oriMap.remove(key);
                oriMap.put(key,newMap.get(key));
                resultList.add(key);
            }
        }
        if (resultList.isEmpty()) {
            return null;
        }
        BlogDeal temp = BeanUtil.mapToBean(oriMap, BlogDeal.class, false);
        return temp;
    }

    public void handlerImgs() {
        if (!StrUtil.isBlank(this.images)) {
            List<String> imgsList = StrUtil.split(images, ',');
            StringBuilder tmpImages = new StringBuilder();
            for (int i = 0;i<imgsList.size();i++) {
                String s = imgsList.get(i);
                String tmpUrlSuffix = COSUtils.getSign(s, HttpMethodName.GET);
                if (i>0) tmpImages.append(',');
                tmpImages.append(tmpUrlSuffix);
            }
            this.imgsUrl = tmpImages.toString();
        }
        this.iconUrl = COSUtils.getSign(this.icon,HttpMethodName.GET);
    }
}
